package org.example.mapreduce;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.VLongWritable;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * @author zhl
 * @version 1.0
 * @ClassName
 * @date 2020/11/10 19:46
 */
public class FlowWritable implements WritableComparable<FlowWritable> {
    private VLongWritable up;
    private VLongWritable down;
    private Text phone;
    private VLongWritable sum;

    public Text getPhone() {
        return phone;
    }

    public void setPhone(Text phone) {
        this.phone = phone;
    }

    public VLongWritable getSum() {
        return sum;
    }

    public void setSum(VLongWritable sum) {
        this.sum = sum;
    }

    public VLongWritable getUp() {
        return up;
    }

    public void setUp(VLongWritable up) {
        this.up = up;
    }

    public VLongWritable getDown() {
        return down;
    }

    public void setDown(VLongWritable down) {
        this.down = down;
    }

    public FlowWritable() {
        this.phone = new Text();
        this.set(new VLongWritable(), new VLongWritable());
    }

    public FlowWritable(String phone, long up, long down) {
        this.phone = new Text(phone);
        this.set(new VLongWritable(up), new VLongWritable(down));
    }

    public FlowWritable(VLongWritable up, VLongWritable down) {
        this.set(up, down);
    }

    public void set(VLongWritable fld1, VLongWritable fld2){
        this.up = fld1;
        this.down = fld2;
        this.sum = new VLongWritable(fld1.get() + fld2.get());
    }

    @Override
    public void write(DataOutput out) throws IOException {
        phone.write(out);
        up.write(out);
        down.write(out);
        sum.write(out);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        phone.readFields(in);
        up.readFields(in);
        down.readFields(in);
        sum.readFields(in);
    }

    @Override
    public String toString() {
        return phone + "\t" + up + "\t" + down + "\t" + sum;
    }

    @Override
    public int compareTo(FlowWritable other) {
        return (int) (sum.get() - other.getSum().get());
    }
}
